<?php

namespace app\api\controller\app;

use addons\epay\library\Service;
use app\admin\model\Article;
use app\admin\model\Articletype;
use app\admin\model\City;
use app\admin\model\Cmdlog;
use app\admin\model\Devicelock;
use app\admin\model\Org;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdevice;
use app\admin\model\Shopjoin;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shoptuan;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Userscore;
use app\admin\model\Usertuan;
use app\admin\model\Userweixin;
use app\api\service\DeviceService;
use app\api\service\LockService;
use app\api\service\RoomService;
use app\api\service\VbillService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;
use Yansongda\Pay\Pay;
use function EasyWeChat\Kernel\Support\get_client_ip;

/**
 */
class Score extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }

    public function userScore(){

        $user = $this->auth->getUser();
        $shop_id = $this->request->param('shop_id');
        $userScoreModel = new Userscore();
        $inScore = $userScoreModel->where('user_id',$user['id'])->where('shop_id',$shop_id)->where('type','in')->sum('score');
        $outScore = $userScoreModel->where('user_id',$user['id'])->where('shop_id',$shop_id)->where('type','out')->sum('score');

        $score = $inScore-$outScore;
        $this->success('',$score);

    }


    public function scoreList(){

        $page = $this->request->param('page');
        $user = $this->auth->getUser();
        $type = $this->request->param('type');
        $where = array();
        $where['Userscore.user_id'] = $user['id'];

        if($type != ""){
            $where['type'] =trim($type);
        }


        $limit = 10;
        $offset = ($page - 1) * $limit;

        error_log("=score=where===".json_encode($where));
        $userScoreModel = new Userscore();
        $list = $userScoreModel->with(['order'])->where($where)->limit($offset,$limit)->select();

        foreach ($list as $key=>$val){


            $list[$key]['create_time_txt'] = date("Y-m-d h:i",$val['create_time']);

        }
        $this->success('成功',$list);

    }

}
